package com.example.maple.ccsclient.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.model.LackModel;
import com.example.maple.ccsclient.utils.DisplayUtils;

import java.util.List;

/**
 * Created by maple on 2016/7/6.
 */
public class LackAdapter extends ArrayAdapter<LackModel> {
	LayoutInflater mLayoutInflater = null;
	int mResource;
	Context mContext;
	int mColumn;
	public LackAdapter(Context context, int resource, List<LackModel> objects,int column) {
		super(context, resource, objects);
		mResource = resource;
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mColumn = column;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if(convertView == null){

			holder = new ViewHolder();
			convertView = mLayoutInflater.inflate(mResource,null);

			// 根据列数计算项目宽度，以使总宽度尽量填充屏幕
			int itemWidth = (mContext.getResources().getDisplayMetrics().widthPixels - (mColumn +1) * DisplayUtils.dip2px(mContext,2) - DisplayUtils.dip2px(mContext,16)) / mColumn;

			// 下面根据比例计算您的item的高度，此处只是使用itemWidth
			int itemHeight = itemWidth;
			AbsListView.LayoutParams param = new AbsListView.LayoutParams(
					itemWidth,
					itemHeight);
			convertView.setLayoutParams(param);

			holder.lackChar = (TextView) convertView.findViewById(R.id.lackChar);
			holder.lackNum = (TextView) convertView.findViewById(R.id.lackNum);
			convertView.setTag(holder);

		}else{
			holder =(ViewHolder) convertView.getTag();
		}

		LackModel info = getItem(position);
		holder.lackChar.setText(info.getChar());
		holder.lackNum.setText(info.getNum());

		return convertView;
	}

	static class ViewHolder{
		TextView lackChar,lackNum;
	}
}
